﻿using PLC.Interface.BaseUsers;
using PLCWpfTest.Common;
using System.Threading.Tasks;
using YWM.Common.Exceptions;
using YWM.Dto;

namespace PLCWpfTest.ViewModels
{
    public class UserPasswordViewModel : OperationViewModel
    {
        private readonly IBaseUserAppService _baseUserAppService;

        public UserPasswordDto UserPassword { get; set; } = new UserPasswordDto();

        public UserPasswordViewModel(IBaseUserAppService baseUserAppService, long id)
        {
            _baseUserAppService = baseUserAppService;
            UserPassword.Id = id;
            IsAdd = false;
        }

        protected override bool OkCommand_BeforeCanExecute(object arg)
        {
            return UserPassword.IsValidated;
        }

        protected override async Task<IEntityDto> OkAsync(object parameter)
        {
            UserPassword.Error = string.Empty;
            await _baseUserAppService.ChangePassword(UserPassword);
            RaiseRequestClose();
            return UserPassword;
        }

        protected override void OkCommand_UnexecutedFriendlyException(FriendlyException ex)
        {
            UserPassword.Error = ex.Message;
        }
    }
}
